package eduAndExpRequire;

import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.IntWritable;

import java.io.IOException;

public class EduAndExpReducer extends Reducer<Text, IntWritable, Text, Text> {

    private static Text outValue = new Text();

    @Override
    public void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        int range1 = 0;
        int range2 = 0;
        int range3 = 0;

        for (IntWritable value : values) {
            int years = value.get();
            if (years < 3) {
                range1++;
            } else if (years < 5) {
                range2++;
            } else {
                range3++;
            }
        }

        String result = String.format("0-2年: %d, 3-5年: %d, 5年以上: %d (总计: %d)",
        range1, range2, range3, range1 + range2 + range3);

        outValue.set(result);
        context.write(key, outValue);
    }
}
